package com.wf.ew.system.service.impl;

import com.wf.ew.common.JsonResult;
import com.wf.ew.common.PageResult;
import com.wf.ew.common.ServiceSupport;
import com.wf.ew.system.biz.DictionaryBiz;
import com.wf.ew.system.model.Dictionary;
import com.wf.ew.system.model.User;
import com.wf.ew.system.service.DictionaryService;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * 数据服务层接口实现类
 * @author : Doman
 * @date : 2019-08-06 16:04:44
 * @version ：1.0
 */
@Service
public class DictionaryServiceImpl extends ServiceSupport implements DictionaryService {

	@Autowired
	private DictionaryBiz dictionaryBiz;

	@Transactional
	@Override
	public String list(PageResult<T> page, Dictionary dictionary) {
		return dictionaryBiz.list(page,dictionary);
	}

	@Transactional
	@Override
	public Dictionary getById(Dictionary dictionary) {
		return dictionaryBiz.getById(dictionary);
	}

	@Transactional
	@Override
	public JsonResult add(Dictionary dictionary, User loginUser) {
		if (dictionaryBiz.add(dictionary, loginUser)) {
			return JsonResult.ok("添加成功");
		}
		return JsonResult.error("添加失败");
	}

	@Transactional
	@Override
	public JsonResult update(Dictionary dictionary, User loginUser) {
		if (dictionaryBiz.update(dictionary, loginUser)) {
			return JsonResult.ok("修改成功");
		}
		return JsonResult.error("修改失败");
	}

	@Transactional
	@Override
	public JsonResult delete(Dictionary dictionary, User loginUser) {
		if (dictionaryBiz.delete(dictionary, loginUser)) {
			return JsonResult.ok("删除成功");
		}
		return JsonResult.error("删除失败");
	}

	@Transactional
	@Override
	public JsonResult dict_code_unique(Dictionary dictionary) {
		String id = dictionary.getId();
		String dict_code = dictionary.getDict_code();
		dictionary = dictionaryBiz.getById(dictionary);
		if(id==null){
			if(dictionary==null){
				return JsonResult.ok("可以使用字典主键");
			} else {
				return JsonResult.error("请保持字典主键唯一性");
			}
		} else {
			Dictionary dictionary2 = new Dictionary();
			dictionary2.setDict_code(dict_code);
			dictionary = dictionaryBiz.getById(dictionary2);
			if(dictionary==null){
				return JsonResult.ok("可以使用字典主键");
			} else {
				return JsonResult.error("请保持字典主键唯一性");
			}
		}

	}
}
